#include "callback.h"
#include "Com_format.h"
#include "Com_tcp.h"
#include "drive.h"
#include "serial.h"
//小车线程函数
void *car_callback()
{
    struct activate_car data_car_temp;
    // 获取串口描述符，用于控制小车行动
    int ttyfd = uart_init();
    while (1)
    {
        int direction = 0;
        if (data_car_update == 1)
        {
            pthread_mutex_lock(&data_car_mutex);
            data_car_temp = data_car;
            data_car_update = 0;
            pthread_mutex_unlock(&data_car_mutex);
            if (data_car_temp.F != 0)
            {
                if (direction != (int)data_car_temp.degrees)
                {
                    direction = (int)data_car_temp.degrees;
                    if (data_car_temp.degrees >= 45 && data_car_temp.degrees < 135)
                    {
                        // 向右转弯
                        write(ttyfd, cmd_TURN_RIGHT, sizeof(cmd_TURN_RIGHT));
                        // write(ttyfd, cmd_ADVANCE, sizeof(cmd_ADVANCE));
                    }
                    if (data_car_temp.degrees >= 225 && data_car_temp.degrees < 315)
                    {
                        // 向左走
                        write(ttyfd, cmd_TURN_LEFT, sizeof(cmd_TURN_LEFT));
                        // write(ttyfd, cmd_ADVANCE, sizeof(cmd_ADVANCE));
                    }
                }
                if (data_car_temp.degrees >= 135 && data_car_temp.degrees < 225)
                {
                    // 倒退
                    write(ttyfd, cmd_RETREAT, sizeof(cmd_RETREAT));
                    // write(ttyfd, cmd_ADVANCE, sizeof(cmd_ADVANCE));
                }
                //命令控制小车行动
                if (data_car_temp.degrees >= 315 || data_car_temp.degrees < 45)
                {
                    // 向前走
                    write(ttyfd, cmd_ADVANCE, sizeof(cmd_ADVANCE));
                }
            }
            else
            {
                // 没有力度小车停止
                write(ttyfd, cmd_STOP, sizeof(cmd_STOP));
            }
        }
        // printf("car:%0.2f-%0.2f\n", data_car.degrees, data_car.F);
    }

// ERROR:
//     close(ttyfd);
//     pthread_mutex_destroy(&data_trumpet_mutex);
}
//蜂鸣器控制线程函数
void *trumpet_callback()
{
    while (1)
    {
        pthread_mutex_lock(&data_trumpet_mutex);
        data_trumpet_update = data_trumpet.mode;
        pthread_mutex_unlock(&data_trumpet_mutex);
        if (1 == data_trumpet_update)
        {
            pwm0_on(200);
        }
        // printf("mode:%d\n", data_trumpet.mode);
    }
    pthread_mutex_destroy(&data_car_mutex);
}
//数据线程接收函数
void *recv_callback(void *sock)
{
    char *COM = NULL;
    printf("进去等待读取\n");
    while (1)
    {
        struct pthread_data data = *(struct pthread_data *)sock;
        datarecv(data.sockfd, &COM);
        printf("读取完毕\n");
        struct head head_data;
        unPackCmdHead(&head_data, COM);
        printf("%d-%lld-%d\n", head_data.data_type, head_data.size, head_data.cmd_type);
        if (head_data.cmd_type == TRUMPET)
        {
            unPackCmdData_trumpet(&data_trumpet, COM);
            data_trumpet_update = 1;
        }
        else if (head_data.cmd_type == CAR_BEHAVIOR)
        {
            //printf("112:%f-%f\n", data_car.degrees, data_car.F);
            pthread_mutex_lock(&data_car_mutex);
            unPackCmdData_activate_car(&data_car, COM);
            data_car_update = 1;
            pthread_mutex_unlock(&data_car_mutex);
        }
        free(COM);
    }
}